import Vue from 'vue'
import Vuex from 'vuex'
import * as index from '@/api/index.js'
Vue.use(Vuex)
const store = new Vuex.Store({
     state: {
     	Type: uni.getStorageSync('Type')||0,
		conversations:'',
		// CDN基础URL
		CDN_BASE_URL: 'https://cdn.xnh.wanhedashuju.com/',
		// 订单列表数据
		orderList: [],
		// 订单详情数据
		orderDetail: null,
		// 订单状态：0-全部，1-待支付，2-已支付，3-已完成，4-已取消
		currentOrderStatus: 0
    },
	mutations: {
		setRoleId(state, data) {
			state.Type = data;
			uni.setStorageSync('Type',data)
		},
		converList(conver, data) {
			conver.conversations = data;
		},
		// 设置订单列表
		setOrderList(state, data) {
			state.orderList = data;
		},
		// 设置订单详情
		setOrderDetail(state, data) {
			state.orderDetail = data;
		},
		// 设置当前订单状态筛选
		setCurrentOrderStatus(state, status) {
			state.currentOrderStatus = status;
		}
	},
	actions: {
		// 获取订单列表
		fetchOrderList({ commit }, params) {
			return new Promise((resolve, reject) => {
				index.orderList(params).then(res => {
					commit('setOrderList', res.page.list);
					resolve(res);
				}).catch(error => {
					reject(error);
				});
			});
		},
		// 获取订单详情
		fetchOrderDetail({ commit }, orderNo) {
			return new Promise((resolve, reject) => {
				// 假设存在获取订单详情的API
				index.orderDetail({ orderNo }).then(res => {
					commit('setOrderDetail', res);
					resolve(res);
				}).catch(error => {
					reject(error);
				});
			});
		}
	},
	getters: {
		// 获取CDN基础URL的getter
		CDN_BASE_URL: state => state.CDN_BASE_URL,
		// 根据类型获取订单列表
		filteredOrderList: (state) => {
			if (state.currentOrderStatus === 0) {
				return state.orderList;
			}
			return state.orderList.filter(order => order.type === state.currentOrderStatus);
		},
		// 获取订单总数量
		totalOrderCount: (state) => state.orderList.length
	}
})

// 注意：为了避免循环依赖，这里不直接导入API，实际使用时需要在组件中引入
export default store